package org.apache.doris.demo.spring.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.doris.demo.spring.model.EvenData;
import org.apache.doris.demo.spring.model.UserTag;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class UIController {

//
@RequestMapping("/")
public String index(Model model){
    int value=0;
    List tagCloudList = new ArrayList();
    for (int i = 0; i <10 ; i++) {
        Map<String, Object> data = new HashMap<>();
        value +=  1000;
        data.put("name", "myname"+i);
        data.put("value", value);
        tagCloudList.add(data);
    }
    List<Object[]> data = new ArrayList<>();
    double base = System.currentTimeMillis();
    double oneDay = 24 * 60 * 60 * 1000;
    Random random = new Random();
    data.add(new Object[] {base, random.nextLong() * 300});
    for (int i = 1; i < 20000; i++) {
        double now = base += oneDay;
        data.add(new Object[] {now, Math.round((random.nextLong() - 0.5) * 20 )});
    }
    String[] timelineData = {"2021", "2022", "2023"};
    List<EvenData> eventData = new ArrayList<>();
    eventData.add(new EvenData("2021年售假被处罚", "2021"));
    eventData.add(new EvenData("2022年被刑拘", "2022"));
    eventData.add(new EvenData("2023年被处罚", "2023"));
    model.addAttribute("data", JSON.toJSONString(data));
    model.addAttribute("timelineData", timelineData);
    model.addAttribute("eventData", eventData);
    model.addAttribute("tagCloudList", JSON.toJSONString(tagCloudList));
//    return "userTag/index";
   return  "area-time-axis";
//   return  "userprofile";
}

@RequestMapping("/tagCloud")
    public String tagCloud(){
        return "index";
    }
@RequestMapping("/circleSelect")
public String tags(){
    return "tags";
}
   @ResponseBody
    @GetMapping("/data")
    public List<Map<String, String>> getData() {
        List<Map<String, String>> result = new ArrayList<>();

        result.add(Collections.singletonMap("name", "男性"));
        result.add(Collections.singletonMap("name", "女性"));
        result.add(Collections.singletonMap("name", "IOS"));

        return result;
    }
//    @RequestMapping("/gen")
//    public void genAndDown(HttpServletResponse response, @RequestBody String data) {
//        JSONObject object = JSON.parseObject(data);
//        JSONArray selectedTags = object.getJSONArray("selectedTags");
//        List<EsQueryTag> list = selectedTags.toJavaList(EsQueryTag.class);
//
//// 调用服务，按照查询限制提取出对应的用户信息
//        List<UserTagEtl.MemberTag> tags = service.searchMemberTags(list);
//
//// 自定义方法，将所有用户信息拼在一起写入文件
//        String content = toContent(tags);
//        String fileName = "member.txt";
//
//        response.setContentType("application/octet-stream");
//        try {
//            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        try {
//            ServletOutputStream sos = response.getOutputStream();
//            BufferedOutputStream bos = new BufferedOutputStream(sos);
//            bos.write(content.getBytes("UTF-8"));
//            bos.flush();    // 直接将缓冲池中数据刷出，默认是要填满才发
//            bos.close();
//            sos.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private String toContent(List<UserTagEtl.MemberTag> tags) {
//        StringBuilder sb = new StringBuilder();
////        for (UserTagEtl.MemberTag tag : tags) {
////            sb.append("[" + tag.getMemberId() + "," + tag.getPhone() + "]\r\n");
////        }
//        return sb.toString();
//    }

}
